package com.kj.mynote.Util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * @author wy
 * @version V1.0
 * @Title: TimeNote
 * @Package com.kj.mynote.Util
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 17/3/23 14:59
 */

public class FileUtils {

    // 根路径
    public static final String rootDir = Environment
            .getExternalStorageDirectory() + "/com.tn/";
    public static final String rootTxtDir = rootDir + "txt/";
    public static final String rootExcelDir = rootDir + "excel/";
    public static final String noteMRDir = rootTxtDir + "默认分组/";

    public static void createFile(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    public static boolean createFolder(File folder) {
        boolean flag = false;
        if (!folder.exists()) {
            flag = folder.mkdirs();
        } else {
            Log.e("---", "文件夹已创建");
            return true;
        }
        if (flag) {
            Log.e("---", "创建成功");
        } else {
            Log.e("---", folder.getAbsolutePath() + "创建失败");
        }
        return flag;
    }

    public static void copyFile() {
    }

    public static void deleteFile(File file) {
        if (file.exists()) {
            file.delete();
        }
    }

    public static void deleteFolder() {
    }

    public static String readFile(File file) {
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                sb.append(tempString + "\n");
                line++;
            }
            reader.close();
        } catch (IOException e) {
            Log.e("e", e.toString());
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    Log.e("e1", e1.toString());
                }
            }
        }
        return sb.toString();
    }

    public static boolean writeFile(String str, String dir, String fileName) {
        try {
            File d = new File(rootTxtDir + dir);
            if (!d.exists()) {
                d.mkdirs();
            }
            File file = new File(rootTxtDir + dir + "/" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream outSTr = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = null;
            bufferedOutputStream = new BufferedOutputStream(outSTr);
            long begin0 = System.currentTimeMillis();
            bufferedOutputStream.write(str.getBytes());
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            long end0 = System.currentTimeMillis();
            System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 豪秒");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**
     * Checks if the app has permission to write to device storage
     * If the app does not has permission then the user will be prompted to
     * grant permissions
     *
     * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
